	/**
	 * 工艺指令JS文件
	 */

	$(document).ready(function(){
			listCheck();//选择行
			listAdd();//增加一行
			listEdit();//编辑选中的行
			listDel();//删除删除选中的行
		});
		
		var trSelected=parseInt($("#trSelected").val());//选中的行数统计
		
		function listCheck(){
			$("#co-listBody tr td:last-child").on("click",function(){
				var thisItem=$(this);
				var itemParent=thisItem.parent();
				
				if(!(itemParent.hasClass("nowCheck"))){
					itemParent.addClass("nowCheck");
					thisItem.find("i").attr("class","fa fa-check-circle");
					
					trSelected+=1;
					$("#trSelected").val(trSelected);
					
				}else{
					itemParent.removeClass("nowCheck");
					thisItem.find("i").attr("class","fa fa-circle-o");
					
					trSelected-=1;
					$("#trSelected").val(trSelected);
				}
			});
		}
		
		function listAdd(){
			$("#co-listAdd").on("click",function(){
				$("#craftModal").modal('show');
			});
		}
		
		function listEdit(){
			$("#co-listEdit").on("click",function(){
				var trSelected=parseInt($("#trSelected").val());
				if(trSelected == 0){
					jAlert("请先选中行，再进行编辑操作","提示");	 
				}else{
					var listTrEdit=$("#co-listBody tr.nowCheck");
					listTrEdit.find("textarea").attr("disabled",false);
					listTrEdit.find("input").attr("disabled",false);
				}
			});
		}
		
		var instructionsArray = new Array();
		
		function saveEditInstructions(type){
			
			var isRepete=false;
			var repeteNo;
//			var listTrEdit=$("#container").find("tr.nowCheck");
			var listTrEdit=$("#container").find("tr.tr_instructions");
			$.each(listTrEdit, function(n,value){
				var instructionsid=$(value).find("input#instructionsid").val();
				var instructionsno=$(value).find("input#instructionsno").val();
				var content=$(value).find("textarea#content").val();

				
				instructionsArray.push({id: instructionsid, 
					                    instructionsno: instructionsno, 
					                    content: content});
	
				$.each(listTrEdit, function(n1,value1){
					var instructionsid1=$(value1).find("input#instructionsid").val();
					var instructionsno1=$(value1).find("input#instructionsno").val();
                    if(instructionsid!=instructionsid1 && instructionsno==instructionsno1){
                    	
                    	isRepete=true;
                    	repeteNo=instructionsno;
                    	return false;
                    }
					 
				});
	            if(isRepete){
	            	return false;
	            }
				
			});
			
              if(isRepete){
	             	jAlert("工艺号"+repeteNo+"和已有的工艺号重复",'提示');
	            	return false;
              }
			
			  if(instructionsArray.length==0  && type==0){
				 
				  jAlert("请至少选择一条记录","提示");
				  return false;
				  
			  }else if(instructionsArray.length==0 && type==1){
				  window.location.href=CONTEXT_PATH+'front/carDesign/carDesign_list';
				  return false;
			  }
			  
	    	  jConfirm('确定要保存吗?', '操作确认', function(r) {
		    	  
		    	     if(r){
		    	           $.ajax({
		    	        	    url:   CONTEXT_PATH+'front/carDesign/saveEditInstructions', 
		    	                type:  "POST",
		    	                contentType : 'application/json;charset=utf-8', //设置请求头信息
		    	                dataType:"json",
		    	                data:  JSON.stringify(instructionsArray),            
		    	                success: function(data){
		    	                    if(data.result){
		    	                    	jAlert(data.msg,'提示', function(r) {
			    	                    	if(type==1){
			    	                    			window.location.href=CONTEXT_PATH+'front/carDesign/carDesign_list';
			    	                    	}else{
			    	                    		window.location.reload();
			    	                    	}
		    	                    	});
		    	                    	
					                }else{
					                	jAlert(data.msg,'提示');
					                }
		    	                    
		    	                } 
		    	          });
		    	    }
	
			  });
		}
		
		function exportPDF(designid){
			
			window.location.href=CONTEXT_PATH+'front/carDesign/exportPDF?type=2&designid='+designid;
		}
		
		
		function listDel(){
			$("#co-listDel").on("click",function(){
				
				var listTrDel=$("#co-listBody tr.nowCheck");
				var idArr=new Array();
				$.each(listTrDel, function(n,value){
					var delid=$(value).find("input#instructionsid").val();
					idArr.push(delid);
		        });
				
				
				if(idArr.length == 0){
					alert("请先选中行,再进行删除操作。");
				}else{
				
			    	  jConfirm('确定要删除吗?', '操作确认', function(r) {
				    	  
				    	     if(r){
				    	     	  $.ajax({
								     type: 'post',
								     url:   CONTEXT_PATH+'front/carDesign/delCarInstructions', 
									 data:  {"ids":idArr.join(",")},
									 dataType: 'json',
									 success: function(data) {  
						                if(data.result){
						                	//window.location.reload();
						            		var listTrDel=$("#co-listBody tr.nowCheck");
						    				for(i=0;i<listTrDel.length;i++){
						    					listTrDel[i].remove();
						    				}
						                }else{
						                	jAlert(data.msg,'提示');
						                }
						             }
									
								 });
				    	     }
			
					    });
					
				}
			});
		}
		